/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-13
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Distribution;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.DistributionItemService;
import com.rzico.basics.service.DistributionService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分销设置控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-13
 */
@Api(description = "分销设置接口")
@RestController
@RequestMapping("/admin/distribution")
public class DistributionAdminController extends BaseController {

    @Autowired
    private DistributionService distributionService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private DistributionItemService distributionItemService;

    /**
     * 分页查询分销设置
     *
     * @return
    */
    @ApiOperation("分页查询分销设置")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型", dataType = "String", paramType = "query")
    })
    public CommResult<Distribution> list(Integer type, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (type!=null){
            params.put("type", type);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Distribution> list = distributionService.selectList(params);
        PageResult<Distribution> pageResult = new PageResult<Distribution>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条分销设置
     *
     * @return
    */
    @ApiOperation("查询单条分销设置")
    @GetMapping("/find/{id}")
    public CommResult<Distribution> find(@PathVariable String id) {
        Distribution result = distributionService.findById(id);

        result.setItemList(distributionItemService.getItemList(result.getId()));

        Map<String,Object> data = new HashMap<>();
        data.put("distribution",result);
        return CommResult.success(data);

    }

    /**
     * 保存分销设置
     *
     * @param distribution
     * @return
     */
    @ApiOperation("保存分销设置")
    @PostMapping("/save")
    public CommResult<Distribution> save(@RequestBody Distribution distribution) {

        Enterprise enterprise = enterpriseService.getCurrent();

        if (enterprise!=null) {
            distribution.setEnterpriseId(enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        if (distribution.getIsAchieve()==null) {
            distribution.setIsAchieve(true);
        }
        if (distribution.getAgainRate()==null) {
            distribution.setAgainRate(BigDecimal.ZERO);
        }
        if (distribution.getPeerRate()==null) {
            distribution.setPeerRate(BigDecimal.ZERO);
        }

        int affectCount = distributionService.save(distribution);
        if (affectCount <= 0) {
            return CommResult.error();
        }

        Distribution result = distributionService.findById(distribution.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除分销设置
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除分销设置", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除分销设置,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Distribution> del(@PathVariable String ids) {

        try {
            distributionService.deleteByIds(ids.split(","));
        } catch (Exception e) {
            return CommResult.error(e.getMessage());
        }
        return CommResult.success();
    }

    /**
     * 更新分销设置
     *
     * @param distribution
     * @return
     */
    @Log(desc = "更新分销设置", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新分销设置")
    @PostMapping("/update")
    public CommResult<Distribution> update(@RequestBody Distribution distribution) {
        int affectCount = distributionService.update(distribution);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Distribution result = distributionService.findById(distribution.getId());
        return CommResult.success(result);
    }

}
